package com.sc.controller.Vast_controller;

import com.sc.Vast_pojo.domain.DeviceGroup;
import com.sc.Vast_pojo.dto.DeviceGroupPageQueryDTO;
import com.sc.dto.DeviceGroupUpdateReqDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.Vast_service.DeviceGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Vast
 * @version 1.0
 * @function:设备分类管理控制层
 */
@Slf4j
@RestController
@Api(tags = "设备分类管理相关接口")
@RequestMapping("/DeviceGroup")
@CrossOrigin
public class DeviceGroupController {
    @Autowired
    private DeviceGroupService deviceGroupService;
    /**
     * 设备分类管理分页查询
     * @param deviceGroupPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("设备分类分页查询")
    public Result<PageResult> page(DeviceGroupPageQueryDTO deviceGroupPageQueryDTO){
        log.info("设备分类管理分页查询：参数为{}",deviceGroupPageQueryDTO);
        PageResult pageResult=deviceGroupService.pageQuery(deviceGroupPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据分类名称新增分类
     * @param deviceGroup
     * @return
     */
    @PostMapping("/{groupName}")
    @ApiOperation("根据分类名称新增分类")
    public Result save(@PathVariable("groupName") String groupName){
        log.info("新增分类名称：{}",groupName);
        deviceGroupService.save(groupName);
        return Result.success(null,"新增成功");
    }

    /**
     * 设备分类修改
     */
    @PutMapping("/updateDeviceGroup")
    public Result updateDeviceGroup(@RequestBody DeviceGroupUpdateReqDTO requestParam)
    {
        log.info("设备分类修改:{}",requestParam);
        deviceGroupService.updateDeviceGroup(requestParam);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<DeviceGroup> queryGroupById(@PathVariable("id") Integer id)
    {
        log.info("根据id获取设备分类:{}",id);
        DeviceGroup deviceGroup = deviceGroupService.getById(id);
        return Result.success(deviceGroup);
    }

    @DeleteMapping("/{id}")
    public Result deleteGroupById(@PathVariable("id") Integer id)
    {
        log.info("设备分类删除:{}",id);
        deviceGroupService.removeById(id);
        return Result.success();
    }

    @GetMapping("/groupList")
    public Result<List<String>> queryGroupList()
    {
        log.info("获取分组名称集合");
        List<String> groupList = deviceGroupService.queryGroupList();
        return Result.success(groupList);
    }

    /**
     * 直接修改分类名称，并且更新表单剩余信息
     * @param updateGroupName
     * @return
     */
    @PostMapping("/updateGroupName/{updateGroupName}")
    @ApiOperation("修改分类名称")
    public Result update(@PathVariable("updateGroupName")String updateGroupName,Integer id){
        log.info("修改分类名称：{},{}",updateGroupName,id);
        deviceGroupService.update(updateGroupName,id);
        return Result.success(null,"修改成功");
    }

    /**
     * 修改分类管理的状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改分类管理的状态")
    public Result startOrStop(@PathVariable("status")Integer status,Integer id){
        log.info("修改分类管理的状态:{},{}",status,id);
        deviceGroupService.startOrStop(status,id);
        return Result.success(null,"修改成功");
    }

    /**
     * 分类管理删除（包括单一删除和批量删除）
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    @ApiOperation("分类管理删除")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        log.info("分类管理删除,{}",ids);
        deviceGroupService.removeByIds(ids);
        return Result.success();
    }
}
